package com.hustzp.com.xichuangzhu.me;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.XCZBaseFragmentActivity;
import com.hustzp.com.xichuangzhu.utils.Constant;
import com.hustzp.com.xichuangzhu.utils.ImageUtils;
import com.hustzp.com.xichuangzhu.widget.XCRoundRectImageView;
import com.jude.library.imageprovider.ImageProvider;
import com.jude.library.imageprovider.OnImageSelectListener;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/4.
 */
public class UserInfoActivity extends XCZBaseFragmentActivity {

    private XCRoundRectImageView userAvatar;
    private ImageProvider imageProvider;
    OnImageSelectListener listener = new OnImageSelectListener() {
        @Override
        public void onImageSelect() {

        }

        @Override
        public void onImageLoaded(Uri uri) {
            imageProvider.corpImage(uri, 600, 600, new OnImageSelectListener() {
                @Override
                public void onImageSelect() {

                }

                @Override
                public void onImageLoaded(final Uri uri) {
                    try {
                        final AVFile avatar = AVFile.withAbsoluteLocalPath("avatar.jpg", uri.getPath());
                        avatar.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(AVException e) {
                                if (e == null) {
                                    AVUser.getCurrentUser().put("avatar", avatar);
                                    AVUser.getCurrentUser().saveInBackground(new SaveCallback() {
                                        @Override
                                        public void done(AVException e) {
                                            if (e == null) {
                                                userAvatar.setImageURI(uri);
                                            }
                                        }
                                    });
                                } else {
                                    Toast.makeText(UserInfoActivity.this, "上传头像失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        Toast.makeText(UserInfoActivity.this, "生成图片文件失败", Toast.LENGTH_SHORT).show();
                    }

                }

                @Override
                public void onError() {

                }
            });
        }

        @Override
        public void onError() {

        }
    };


    private TextView userName;
    private TextView userDesc;

    private AVUser curUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);

        initToolbar();
        initViews();
        initData();
    }

    private void initToolbar() {
        TextView appTitle = (TextView) findViewById(R.id.tv_title_txt);
        appTitle.setText(getString(R.string.pernalinfomation));
    }

    private void initViews() {
        userAvatar = (XCRoundRectImageView) findViewById(R.id.user_avatar);
        imageProvider = new ImageProvider(this);

        userName = (TextView) findViewById(R.id.tv_user_name);
        userDesc = (TextView) findViewById(R.id.tv_user_desc);
    }

    private void initData() {
        curUser = AVUser.getCurrentUser();
        if (curUser.getAVFile("avatar") == null) {
            ImageUtils.loadImage(Constant.DEFAULT_AVATAR_URL, userAvatar);
        } else {
            ImageUtils.loadImage(curUser.getAVFile("avatar").getUrl(), userAvatar);
        }

        userName.setText(curUser.getUsername());
        userDesc.setText(curUser.getString("desc"));
    }


    @Override
    protected void onResume() {
        super.onResume();

        curUser = AVUser.getCurrentUser();
        initData();
    }

    public void clickView(View view) {
        switch (view.getId()) {
            case R.id.user_avatar:
                // 上传用户头像

                List<String> menus = new ArrayList<>();
                menus.add("拍照");
                menus.add(getString(R.string.pernalinfomation_album));

                initSharePopupWindow();
                showSharePopupWindow("上传头像", menus, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        switch (i) {
                            case 0:
                                imageProvider.getImageFromCamera(listener);
                                break;
                            case 1:
                                imageProvider.getImageFromAlbum(listener);
                                break;
                        }
                        hideAlert();
                    }
                });
                break;
            case R.id.my_nickname_btn:
                // 修改用户名
                Intent data = new Intent(this, EditUserInfoActivity.class);
                data.putExtra("appTitle", getString(R.string.pernalinfomation_editor_name));
                data.putExtra("editFlag", 1);
                data.putExtra("userInfo", userName.getText().toString());
                startActivity(data);
                break;
            case R.id.my_profile_btn:
                // 修改签名
                Intent data2 = new Intent(this, EditUserInfoActivity.class);
                data2.putExtra("appTitle", getString(R.string.pernalinfomation_edit_sigined));
                data2.putExtra("editFlag", -1);
                data2.putExtra("userInfo", userDesc.getText().toString());
                startActivity(data2);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        imageProvider.onActivityResult(requestCode, resultCode, data);
    }
}
